package entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import mailApplication.Mail;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.DateTime;
import ch.ivyteam.ivy.security.IUser;
import ch.ivyteam.ivy.security.SecurityManagerFactory;

@ManagedBean
@ApplicationScoped
public class MailData {
	long idMail = 0;

	private Map<IUser, List<Mail>> mailMap;

	public List<IUser> getAllUsers() throws Exception {

		return SecurityManagerFactory.getSecurityManager().executeAsSystem(
				new Callable<List<IUser>>() {

					public List<IUser> call() throws Exception {

						return Ivy.session().getSecurityContext().getUsers();
					}
				});
	}

	public MailData() {

        mailMap = new HashMap<IUser, List<Mail>>();

        mailMap.put(Ivy.session().getSessionUser(), createMailDummyDataList());
        
        //Ivy.log().error("--------------------sadfasdfasfd------------------" + new Date());
	}

	@PostConstruct
	public void initializeData() {

	}

	private List<Mail> createMailDummyDataList() {

		List<Mail> result = new ArrayList<Mail>();
		for (Integer i = 0; i < 20; i++) {
			result.add(createDummyMailData(i));
		}

		return result;

	}

	private Mail createDummyMailData(Integer refNum) {

		Mail mail = new Mail();
		Random rd = new Random();

		mail.setId(idMail++);

		int randomNum = rd.nextInt(4);
		if (randomNum == 1) {
			mail.setIsDeleted(true);
			mail.setIsDraft(false);
		} else if (randomNum == 2) {
			mail.setIsDeleted(false);
			mail.setIsDraft(true);
		} else {
			mail.setIsDeleted(false);
			mail.setIsDraft(false);
		}

		mail.setMailContent("Mail Content " + refNum.toString());
		mail.setMailFrom("Mail From " + refNum.toString());
		mail.setMailTo("Mail To " + refNum.toString());
		mail.setMailSubject("Mail Subject " + refNum.toString());
		mail.setSendDate(new DateTime());

		return mail;
	}
	
	
	
	public List<Mail> getInboxMail(){
		IUser user = Ivy.session().getSessionUser();
		List<Mail>allMail = getMailOfUser(user);
		List<Mail> inboxMail = new ArrayList<Mail>();
		for (Mail mail : allMail) {
			if(!mail.getIsDeleted()&& !mail.getIsDraft()){
				inboxMail.add(mail);
			}
		}
		return inboxMail;
	}

	public List<Mail> getDeletedMail(){
		IUser user = Ivy.session().getSessionUser();
		List<Mail>allMail = getMailOfUser(user);
		List<Mail> deletedMail = new ArrayList<Mail>();
		for (Mail mail : allMail) {
			if(mail.getIsDeleted()){
				deletedMail.add(mail);
			}
		}
		return deletedMail;
	}
	public List<Mail> getDraftMail(){
		IUser user = Ivy.session().getSessionUser();
		List<Mail>allMail = getMailOfUser(user);
		List<Mail> draftMail = new ArrayList<Mail>();
		for (Mail mail : allMail) {
			if(mail.getIsDraft()){
				draftMail.add(mail);
			}
		}
		return draftMail;
	}
	
	public Map<IUser, List<Mail>> getMailMap() {

		return mailMap;
	}

	public void setMailMap(Map<IUser, List<Mail>> mailData) {

		this.mailMap = mailData;
	}

	// ------- CRUD --------

	public Mail getMailById(IUser user, int id) {
		List<Mail> listMail = getMailOfUser(user);
		for (Mail mail : listMail) {
			if (mail.getId() == id) {
				return mail;
			}
		}
		return null;
	}

	public List<Mail> getMailOfUser(IUser user) {
		return mailMap.get(user);
	}

	public void addNewMailToUser(IUser user, Mail mail) {
		List<Mail> listMail = getMailOfUser(user);
		mail.setId(idMail++);
		listMail.add(mail);
		mailMap.put(user, listMail);
		Ivy.log().info(mailMap);
	}

	public void removeMailById(IUser user, int id) {
		List<Mail> listMail = getMailOfUser(user);
		for (Mail mail : listMail) {
			if (mail.getId() == id) {
				listMail.remove(mail);
			}
		}
		mailMap.put(user, listMail);
	}

    public void removeMail(Long id) {

        List<Mail> mailList = mailMap.get(Ivy.session().getSessionUser());

        for (Mail mail : mailList) {
            if (mail.getId().equals(id)) {
                if (mail.getIsDeleted()) {
                    mailList.remove(mail);
                } else {
                    mail.setIsDeleted(true);
                    mail.setIsDraft(false);
                }
                return;
            }
        }
    }

}
